package com.ymttest.business.service;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.client.ClientProtocolException;

import com.ymatou.iapi.godfreightmgr.parameter.CreateCollectionGoodsAddressBean;
import com.ymatou.iapi.godfreightmgr.parameter.CreateFreightDetailDto;
import com.ymatou.iapi.godfreightmgr.parameter.CreateFreightDto;
import com.ymatou.iapi.godfreightmgr.parameter.CreateFreightTemplateBean;
import com.ymatou.iapi.godfreightmgr.parameter.DeleteCollectionGoodsAddressBean;
import com.ymatou.iapi.godfreightmgr.parameter.DeleteTemplateInfoBean;
import com.ymatou.iapi.godfreightmgr.parameter.EditFreightTemplateBean;
import com.ymatou.iapi.godfreightmgr.parameter.GetCollectionGoodsAddressListBean;
import com.ymatou.iapi.godfreightmgr.parameter.SetCollectionGoodsByAdminBean;
import com.ymatou.iapi.godfreightmgr.parameter.SetCollectionGoodsBySellerBean;
import com.ymatou.iapi.godfreightmgr.parameter.SetExpressDeliveryBySellerBean;
import com.ymatou.iapi.godfreightmgr.parameter.SetFreightCalcModelBean;
import com.ymatou.iapi.godfreightmgr.service.CreateCollectionGoodsAddressCall;
import com.ymatou.iapi.godfreightmgr.service.CreateFreightTemplateCall;
import com.ymatou.iapi.godfreightmgr.service.DeleteCollectionGoodsAddressCall;
import com.ymatou.iapi.godfreightmgr.service.DeleteTemplateInfoCall;
import com.ymatou.iapi.godfreightmgr.service.EditFreightTemplateCall;
import com.ymatou.iapi.godfreightmgr.service.GetCollectionGoodsAddressListCall;
import com.ymatou.iapi.godfreightmgr.service.SetCollectionGoodsByAdminCall;
import com.ymatou.iapi.godfreightmgr.service.SetCollectionGoodsBySellerCall;
import com.ymatou.iapi.godfreightmgr.service.SetExpressDeliveryBySellerCall;
import com.ymatou.iapi.godfreightmgr.service.SetFreightCalcModelCall;
import com.ymttest.database.model.godfreight.CollectiongoodsAddress;
import com.ymttest.database.sqlwapper.godfreight.freight_templateWapper;
import com.ymttest.utils.AgentUsers;
import com.ymttest.utils.TestDataManager;

/**
 * 神器运费模板
 */
public class GodfreightmgrService {

	/**
	 * 
	 * @param sellerId 商户id
	 * @param calcModel 计费方式 1 按件 2 按重
	 * @param saleMode 1-零售直销和代理, 2-直销代理共享
	 * @return
	 */
    public static CreateFreightTemplateBean initCreateFreightTemplate(int sellerId,int calcModel,int saleMode){
        CreateFreightTemplateBean createTemplate = new CreateFreightTemplateBean();
        createTemplate.setSellerId(sellerId);//商家编号
        createTemplate.setTemplateName("测试运费模板"+ TestDataManager.getRandomNum(5));//运费名称

        CreateFreightDto createFreightDto = setCreateFreightDto(calcModel);
        CreateFreightDto createFreightDto1 = setCreateFreightDto(calcModel);

        if (saleMode==1){
            createTemplate.setMainFreight(createFreightDto);//主运费模板信息
            createTemplate.setAgentFreight(createFreightDto1);//代理运费模板信息
            createTemplate.setIsShareTemplate(false);//是否共享模板
        }
        else if (saleMode==2){
            createTemplate.setMainFreight(createFreightDto);
            createTemplate.setIsShareTemplate(true);//是否共享模板
        }

        createTemplate.setPolicyModel(1);//组合策略
        createTemplate.setCurrency("USD");//币种默认CNY 其他币种需要跟商户币种一致
        return createTemplate;
    }

    public static CreateFreightDto setCreateFreightDto(int calcModel){
        CreateFreightDto createFreightDto = new CreateFreightDto();
        createFreightDto.setCalcModel(calcModel);

        CreateFreightDetailDto createFreightDetail = new CreateFreightDetailDto();
        createFreightDetail.setDefaultTemplate(true);//是否默认运费模板
        if (calcModel == 1){//按件计费
            createFreightDetail.setFirstNum(6);//首件数量
            createFreightDetail.setAddNum(2);//续件数量
            createFreightDetail.setFirstFee(new BigDecimal(10));//首件/首重运费
            createFreightDetail.setAddFee(new BigDecimal(5));//续件 / 续重 运费
        }
        if (calcModel == 2){//按重计费
            createFreightDetail.setFirstWeight(new BigDecimal(7));//首件重量
            createFreightDetail.setAddWeight(new BigDecimal(2));//续件重量
            createFreightDetail.setFirstFee(new BigDecimal(15));//首件/首重运费
            createFreightDetail.setAddFee(new BigDecimal(10));//续件 / 续重 运费
        }

        List<CreateFreightDetailDto> templateItems = new ArrayList<>();
        templateItems.add(createFreightDetail);
        createFreightDto.setTemplateItems(templateItems);//运费明细内容
        return createFreightDto;
    }

    /**
     * 编辑运费模板
     * @param createTemplate
     * @param templateId
     * @return
     */
    public static EditFreightTemplateBean initEditFreightTemplateBean(CreateFreightTemplateBean createTemplate,int templateId){
        EditFreightTemplateBean editTemplate = new EditFreightTemplateBean();
        editTemplate.setTemplateId(templateId);
        editTemplate.setMainFreight(createTemplate.getMainFreight());
        editTemplate.setAgentFreight(createTemplate.getAgentFreight());
        editTemplate.setIsShareTemplate(createTemplate.getIsShareTemplate());
        editTemplate.setPolicyModel(createTemplate.getPolicyModel());
        editTemplate.setSellerId(createTemplate.getSellerId());
        editTemplate.setTemplateName(createTemplate.getTemplateName());
        editTemplate.setCurrency(createTemplate.getCurrency());
        return editTemplate;
    }

    /**
     * 编辑运费模板
     * @param editFreightTemplateBean
     * @throws IOException
     */
    public static void EditFreightTemplate(EditFreightTemplateBean editFreightTemplateBean) throws IOException {
        EditFreightTemplateCall editFreightTemplateCall = new EditFreightTemplateCall();
        editFreightTemplateCall.setData(editFreightTemplateBean);
        editFreightTemplateCall.callService();
    }

    /**
     * 创建运费模板
     * @param createTemplate
     * @return
     * @throws IOException
     */
    public static String CreateFreightTemplate(CreateFreightTemplateBean createTemplate) throws IOException {
    	try {
			
		} catch (Exception e) {
			// TODO: handle exception
		}
        CreateFreightTemplateCall createFreightTemplateCall = new CreateFreightTemplateCall();
        createFreightTemplateCall.setData(createTemplate);
        createFreightTemplateCall.callService();
        createFreightTemplateCall.getJsonObject().get("400");
        return createFreightTemplateCall.getJsonObject().get("data").getAsJsonObject().get("templateId").getAsString();
    }

    /**
     * 设置多运费模板计费方式
     * @param sellerId
     * @param policyModel 1累加 2 组合
     * @throws IOException
     */
    public static void SetFreightCalcModel(int sellerId,int policyModel) throws IOException {
        SetFreightCalcModelBean setFreightCalcModelBean = new SetFreightCalcModelBean();
        setFreightCalcModelBean.setSellerId(sellerId);
        setFreightCalcModelBean.setPolicyModel(policyModel);
        SetFreightCalcModelCall setFreightCalcModelCall = new SetFreightCalcModelCall();
        setFreightCalcModelCall.setData(setFreightCalcModelBean);
        setFreightCalcModelCall.callService();
    }

    /**
     * 删除运费模板
     * @param sellerId
     * @param templateId
     * @throws IOException
     */
    public static void DeleteTemplateInfo(int sellerId,int templateId) throws IOException {
        DeleteTemplateInfoBean deletetemplateinfoBean = new DeleteTemplateInfoBean();
        DeleteTemplateInfoCall deletetemplateinfoCall = new DeleteTemplateInfoCall();
        deletetemplateinfoBean.setTemplateId(templateId);
        deletetemplateinfoBean.setSellerId(sellerId);
        deletetemplateinfoCall.setData(deletetemplateinfoBean);
        deletetemplateinfoCall.callService();
    }
    
    
    
    /**
     * 删除运费模板以及商品
     * @param sellerId
     * @param templateId
     * @throws IOException
     */
    public static void DeleteTemplateInfoV2(int sellerId,int templateId,String productId) throws IOException {

    	AddAgentProductCallService.DeleteProduct(productId, sellerId);
    	
        DeleteTemplateInfoBean deletetemplateinfoBean = new DeleteTemplateInfoBean();
        DeleteTemplateInfoCall deletetemplateinfoCall = new DeleteTemplateInfoCall();
        deletetemplateinfoBean.setTemplateId(templateId);
        deletetemplateinfoBean.setSellerId(sellerId);
        deletetemplateinfoCall.setData(deletetemplateinfoBean);
        deletetemplateinfoCall.callService();
    }
    
    /**
     * 删除模版 （freight_template表中的TemplateStatus = -1）
     * @param sellerId
     * @throws IOException
     */
    public static int deleteBySellerId(int sellerId) throws IOException {
    	freight_templateWapper templateWapper = new freight_templateWapper();
    	int count=templateWapper.deleteBySellerId(sellerId);
        return count;
    }
    
    /**
     * 初始化默认createcollectiongoodsaddressBean 自提点
     * @return
     */
    public static CreateCollectionGoodsAddressBean initCreateCollectionGoodsAddressBean(Integer sellerid) {
    	CreateCollectionGoodsAddressBean createcollectiongoodsaddressBean=new CreateCollectionGoodsAddressBean();
    	createcollectiongoodsaddressBean.setSellerId(sellerid);
		createcollectiongoodsaddressBean.setAddressName("AT"+TestDataManager.getRandomNumToHex(8));
		createcollectiongoodsaddressBean.setAddressPic("https://www.shop2cn.com/s1/shop/static/version2/icon1.png");
		createcollectiongoodsaddressBean.setAddressType(1);
		createcollectiongoodsaddressBean.setAreaCode("021");
		createcollectiongoodsaddressBean.setBaseAddress("境外");
		createcollectiongoodsaddressBean.setDetailAddress("2201 Wisconsin Avenue, N.W., Suite 110, Washington, DC 20007");
		createcollectiongoodsaddressBean.setMobile("001-202-4952216");
		return createcollectiongoodsaddressBean;
				
    }
    /**
     * 创建自提点
     * @param createcollectiongoodsaddressBean
     * @throws IOException 
     * @throws ClientProtocolException 
     */
    public static int createcollectiongoodsaddressCall(CreateCollectionGoodsAddressBean createcollectiongoodsaddressBean) throws ClientProtocolException, IOException {
    	CreateCollectionGoodsAddressCall createcollectiongoodsaddressCall=new CreateCollectionGoodsAddressCall();
    	createcollectiongoodsaddressCall.setData(createcollectiongoodsaddressBean);
		createcollectiongoodsaddressCall.callService();	
		return createcollectiongoodsaddressCall.getJsonObject().get("data").getAsJsonObject().get("addressId").getAsInt();
    }
    /**
     * 删除自提点
     * @param addressId
     * @param mchId_channl
     * @throws ClientProtocolException
     * @throws IOException
     */
    public static void deletecollectiongoodsaddressCall(int addressId,int mchId_channl) throws ClientProtocolException, IOException {
    	DeleteCollectionGoodsAddressBean deletecollectiongoodsaddressBean=new DeleteCollectionGoodsAddressBean();
    	DeleteCollectionGoodsAddressCall deletecollectiongoodsaddressCall=new DeleteCollectionGoodsAddressCall();
    	deletecollectiongoodsaddressBean.setAddressId(addressId);
    	deletecollectiongoodsaddressBean.setSellerId(mchId_channl);
    	deletecollectiongoodsaddressCall.setData(deletecollectiongoodsaddressBean);
    	deletecollectiongoodsaddressCall.callService();			
    }
    /**
     * 查询自提点
     * @param sellerid
     * @return 
     * @throws IOException 
     * @throws ClientProtocolException 
     */
    public static ArrayList<CollectiongoodsAddress> getCollectionGoodsAddresslist(Integer sellerid) throws ClientProtocolException, IOException {
    	GetCollectionGoodsAddressListBean getcollectiongoodsaddresslistBean=new GetCollectionGoodsAddressListBean();
    	GetCollectionGoodsAddressListCall getcollectiongoodsaddresslistCall=new GetCollectionGoodsAddressListCall();
    	getcollectiongoodsaddresslistBean.setKeywords(null);
		getcollectiongoodsaddresslistBean.setPageIndex(1);
		getcollectiongoodsaddresslistBean.setPageSize(20);
		getcollectiongoodsaddresslistBean.setSellerId(sellerid);
		getcollectiongoodsaddresslistCall.setData(getcollectiongoodsaddresslistBean);
		getcollectiongoodsaddresslistCall.callService();
		return getcollectiongoodsaddresslistCall.getCollectiongoodsAddress();
    }
    /**
     * 运营设置上门自提功能开关
     * @param sellerid
     * @param enableCollectionGoods
     * @throws IOException 
     * @throws ClientProtocolException 
     */
    public static void setCollectionGoodsByAdminCall(int sellerid,boolean enableCollectionGoods) throws ClientProtocolException, IOException {
    	SetCollectionGoodsByAdminBean setcollectiongoodsbyadminBean=new SetCollectionGoodsByAdminBean();
    	SetCollectionGoodsByAdminCall setcollectiongoodsbyadminCall=new SetCollectionGoodsByAdminCall();
    	setcollectiongoodsbyadminBean.setEnableCollectionGoods(enableCollectionGoods);
    	setcollectiongoodsbyadminBean.setSellerId(sellerid);
    	setcollectiongoodsbyadminCall.setData(setcollectiongoodsbyadminBean);
    	setcollectiongoodsbyadminCall.callService();			
    }
    /**
     * 商家设置上门自提功能开关
     * @param sellerid
     * @param enableCollectionGoods
     * @throws ClientProtocolException
     * @throws IOException
     */
    public static void setCollectionGoodsBySellerCall(int sellerid,boolean enableCollectionGoods) throws ClientProtocolException, IOException {
    	SetCollectionGoodsBySellerBean setcollectiongoodsbysellerBean=new SetCollectionGoodsBySellerBean();
    	SetCollectionGoodsBySellerCall setcollectiongoodsbysellerCall=new SetCollectionGoodsBySellerCall();
    	setcollectiongoodsbysellerBean.setEnableCollectionGoods(enableCollectionGoods);
		setcollectiongoodsbysellerBean.setSellerId(sellerid);
		setcollectiongoodsbysellerCall.setData(setcollectiongoodsbysellerBean);
		setcollectiongoodsbysellerCall.callService();
    }
    /**
     * 商家设置快递开关
     * @param sellerid
     * @param enableCollectionGoods
     * @throws ClientProtocolException
     * @throws IOException
     */
    public static void setExpressDeliveryBySellerCall(int sellerid,boolean enableExpressDelivery) throws ClientProtocolException, IOException {
    	SetExpressDeliveryBySellerBean setexpressdeliverybysellerBean=new SetExpressDeliveryBySellerBean();
    	SetExpressDeliveryBySellerCall setexpressdeliverybysellerCall=new SetExpressDeliveryBySellerCall();
    	setexpressdeliverybysellerBean.setEnableExpressDelivery(enableExpressDelivery);
    	setexpressdeliverybysellerBean.setSellerId(sellerid);
    	setexpressdeliverybysellerCall.setData(setexpressdeliverybysellerBean);
    	setexpressdeliverybysellerCall.callService();
    }
	public static void main(String[] args) throws Exception {
		
		System.out.print(GodfreightmgrService.deleteBySellerId(AgentUsers.rootUserId));
	}
}
